package com.github.ilioili.justdoit.components.patch;

import android.app.Activity;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.github.ilioili.justdoit.BuildConfig;
import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.api.AppUpdateApi;
import com.github.ilioili.justdoit.model.to.VersionInfo;
import com.taihe.template.base.http.Callback;
import com.taihe.template.base.http.Error;

/**
 * Created by User on 16/6/22.
 */
public class UpdateHelper {
    public static void checkUpdate(final Activity activity){
        AppUpdateApi.getVersionInfo(new Callback<VersionInfo>() {
            @Override
            public Object onSucceed(final VersionInfo versionInfo) {
                if(versionInfo.versionCode > BuildConfig.VERSION_CODE){
                    Snackbar snackbar = Snackbar.make(activity.findViewById(Window.ID_ANDROID_CONTENT), "发现现版本"+versionInfo.versionName+",是否更新", Snackbar.LENGTH_INDEFINITE);
                    snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            DownloadHelper.downloadOrInstall(versionInfo, activity);
                        }
                    });
                    snackbar.show();
                }
                return versionInfo;
            }

            @Override
            public void onFail(Error error) {
                Log.e("", error.toString());
            }
        }).execute();
    }
}
